/*
 * @Date: 2020-07-15 16:54:55
 * @LastEditTime: 2020-07-16 15:21:40
 * @Description: 时间方法
 * @FilePath: \vue\src\common\utilTime.js
 */
import moment from 'moment'
import { Type } from 'vue-leng/src/utils/utils'

const dateDiff = function (start, end) {
  return Math.abs(moment(end).diff(moment(start).format('YYYY-MM-DD'), 'days'))
}

const formDate = function (start) {
  return moment(start).format("MM-DD")
}

const formDateDay = function (start) {
  return moment(start).format("M月D")
}

const formatTimeMMDD = function (time) {
  return moment(time).format('MM-DD')
}

const timeDate = function (time) {
  return moment(time).format('HH:mm')
}

const formatDate = function (time) {
  return moment(time).format('YYYY-MM-DD')
}
/**
 * 获取星期
 * @param week
 * @return {array || string}
 */
const getWeekFormat = function (week) {
  let weeks = ['日', '一', '二', '三', '四', '五', '六']
  return Type.isNumber(week) ? weeks[week] : weeks
}

/**
 * @description: 返回周几
 * @param {type} 
 * @return: 
 */
const getZhWeek = function (targetTime) {
  let weekText = dateDiff(targetTime, moment().format('YYYY-MM-DD'))
  switch (weekText) {
    case 0:
      return '今天'
    case 1:
      return '明天'
    default:
      return `星期${getWeekFormat(moment(targetTime).day())}`
  }
}

/**
 * 将日期重置返回时间戳
 * @param time
 * @return {number}
 */
const clearHours = function (time) {
  const cloneDate = new Date(time)
  cloneDate.setHours(0, 0, 0, 0)
  return cloneDate.getTime()
}

//时间戳转换
const timestamp = function (date, fmt) {
  var o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    "S": date.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + ""));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

/**
 * 添加日期天数
 * @param currentDate
 * @param days
 * @return {Date}
 */
const addDate = function (currentDate, days) {
  if (days == undefined || days == '') {
    days = 1
  }
  let date = new Date(currentDate)
  date.setDate(date.getDate() + days)
  return date
}

/**
 * 判断某个时间是否在指定的时间范围
 * @param time 需要判断的时间
 * @param a 指定范围的开始花间
 * @param b 指定范围的结束时间
 * @return {boolean}
 */
const isInRange = (time, a, b) => {
  if (!a || !b) return false
  const [start, end] = [a, b].sort()
  return time >= start && time <= end
}

/**
 * 比较2个时间的大小
 * @param targetDate
 * @param currentDate
 * @return {boolean}
 */
const isBeforeDate = function (targetDate, currentDate) {
  const a = new Date(targetDate)
  const b = new Date(currentDate)
  return clearHours(b) > clearHours(a)
}

// 根据年份判断是否闰年
const isRun = function(yea) {
  if (yea % 400 === 0) return true
  if (yea % 100 === 0) return false
  return yea % 4 === 0
}

// 根据年月判断当月日期数
const daysOfMonth = function(mon, yea) {
  return {0: 31, 1: null, 2: 31, 3: 30, 4: 31, 5: 30, 6: 31, 7: 31, 8: 30, 9: 31, 10: 30, 11: 31}[mon] ||
    (this.isRun(yea) ? 29 : 28)
}

const dateFresh = function(time, callback, total = 30, currentTime) {
  if (typeof time == 'string') {
    time = +moment(time)
  }
  if (typeof currentTime == 'string') {
    currentTime = +moment(currentTime)
  }
  let timer = null
  let nowtime = currentTime
  timer = setInterval(function () {
    let datetext = ''
    let jias = total * 60
    let endtime = new Date(time)
    nowtime += 1000
    let leftsecond = parseInt((endtime.getTime() - nowtime) / 1000 + jias)
    let m = parseInt((leftsecond / 60) % 60)
    let s = parseInt(leftsecond % 60)
    if (m < 10) {
      m = '0' + m
    }
    if (s < 10) {
      s = '0' + s
    }
    if (leftsecond <= 0) {
      clearInterval(timer)
      datetext = '0'
      callback && callback()
      callback(datetext)
    } else {
      datetext = `${m}分${s}秒`
      callback && callback()
      callback(datetext)
    }
  }, 1000)
}

export {
  formatTimeMMDD,
  dateDiff,
  getZhWeek,
  formDate,
  timeDate,
  formatDate,
  formDateDay,
  timestamp,
  clearHours,
  addDate,
  isInRange,
  isBeforeDate,
  isRun,
  daysOfMonth,
  dateFresh
}